package com.wan.adapter

import android.support.v4.util.SparseArrayCompat
import android.support.v7.widget.RecyclerView
import android.view.View
import android.view.ViewGroup

/**
 * 添加头布局和尾部局的 适配器
 */
class HeaderAndFooterAdapter(private var adapter: RecyclerView.Adapter<RecyclerView.ViewHolder>) : RecyclerView.Adapter<RecyclerView.ViewHolder>() {

    //头布局
    private var headerViewCompat: SparseArrayCompat<View> = SparseArrayCompat()
    // 尾布局
    private var footerViewCompat: SparseArrayCompat<View> = SparseArrayCompat()

    private var headerInitPosition: Int = 100000
    private var footerInitPosition: Int = 200000

    private var recyclerView: RecyclerView? = null

    override fun getItemViewType(position: Int): Int {
        if (isHeaderView(position)) {
            return headerViewCompat.keyAt(position)
        }
        if (isFooterView(position)) {
            var index = position - adapter.itemCount - headerViewCompat.size()
            return footerViewCompat.keyAt(index)
        }
        return adapter.getItemViewType(position - headerViewCompat.size())
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerView.ViewHolder {

        if (isHeaderType(viewType)) {
            var headerView = headerViewCompat.get(viewType)
            return object : RecyclerView.ViewHolder(headerView) {}
        }

        if (isFooterType(viewType)) {
            var footView = footerViewCompat.get(viewType)
            return object : RecyclerView.ViewHolder(footView) {}
        }

        return adapter.onCreateViewHolder(parent, viewType)
    }

    override fun getItemCount(): Int {
        return headerViewCompat.size() + adapter.itemCount + footerViewCompat.size()
    }

    override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) {
        if (isFooterView(position) or isHeaderView(position)) {
            return
        }
        if (holder is BaseViewHolder) {
            adapter.onBindViewHolder(holder, position - headerViewCompat.size())
        }
    }

    fun addHeaderView(headerView: View) {
        var indexOfValue = headerViewCompat.indexOfValue(headerView)
        if (indexOfValue < 0) {
            headerViewCompat.put(headerInitPosition++, headerView)
            notifyDataSetChanged()
        }
    }

    fun addFooterView(footerView: View) {
        var indexOfValue = footerViewCompat.indexOfValue(footerView)
        if (indexOfValue < 0){
            footerViewCompat.put(footerInitPosition++,footerView)
            notifyDataSetChanged()
        }
    }

    private fun isHeaderView(position: Int): Boolean {
        return position < headerViewCompat.size()
    }

    private fun isFooterView(position: Int): Boolean {
        return position >= (adapter.itemCount + headerViewCompat.size())
    }

    private fun isHeaderType(viewType: Int): Boolean {

        return headerViewCompat.indexOfKey(viewType) >= 0
    }

    private fun isFooterType(viewType: Int): Boolean {

        return footerViewCompat.indexOfKey(viewType) >= 0
    }

    override fun onAttachedToRecyclerView(recyclerView: RecyclerView) {
        super.onAttachedToRecyclerView(recyclerView)
        this.recyclerView = recyclerView
    }

    override fun onDetachedFromRecyclerView(recyclerView: RecyclerView) {
        super.onDetachedFromRecyclerView(recyclerView)
        this.recyclerView = null
    }

}